<?php

class model_modules extends database_mysql {

    public function md_modules_get_list_app() {
        $strSql = "";
        $tb_com = prefix . 'categories ';
        $tb_com_lang = prefix . "categories_lang";

        $strSql = "SELECT 
                        com.ID, 
                        com.App,
                        col.CatName 
                    FROM 
                        $tb_com AS com ,
                        $tb_com_lang AS col 
                    WHERE
                        com.ID = col.CatID 
                        AND col.`Status` = true 
                        AND com.Define = 'app'                         
                        AND col.LangID = " . LANG;

        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

    public function md_modules_list($appid = 1, $page = 1, $limit = 50) {
        $str = null;
        if (!empty($limit)) {
            $page = ($page == 0 || empty($page) ? 1 : $page);
            $start = ($page - 1) * $limit;
            $strlimit = "LIMIT $start, $limit";
        } else {
            $strlimit = '';
        }

        if ($appid) {
            $appid = parent::mysql_check_string($appid);
            $str = " " . "mou.AppID = '" . $appid . "' AND ";
        }

        $row = parent::mysql_fetch_all_rows("
                SELECT  mou.ID,
                        mou.`Name`, 
                        mol.`Status`, 
                        mou.AppID, 
                        mol.ModuleTitle,                         
                        mol.StrConfig, 
                        mol.Notes
                FROM
                " . prefix . "modules mou INNER JOIN " . prefix . "modules_lang mol
                ON " . "mou.ID = " . "mol.modulesID
                WHERE
                " . $str . "
                " . "mol.LangID = '" . LANG . "'
                ORDER BY " . "mou.ID DESC " . $strlimit . "
        ");
        parent::mysql_close_connection();
        return $row;
    }

    public function md_modules_total_list($appid = 1) {
        $str = null;
        if ($appid) {
            $appid = parent::mysql_check_string($appid);
            $str = " " . prefix . "modules.AppID = '" . $appid . "'  AND ";
        }
        $strSql = "
                SELECT
                count(ID) as tsd
                FROM
                " . prefix . "modules INNER JOIN " . prefix . "modules_lang 
                ON " . prefix . "modules.ID = " . prefix . "modules_lang.modulesID
                WHERE
                " . $str . "
                " . prefix . "modules_lang.LangID = '" . LANG . "'

        ";
        echo $strSql;
        $row = parent::mysql_fetch_all_rows($strSql);

        $tsd = isset($row[0]['tsd']) ? $row[0]['tsd'] : 0;

        parent::mysql_close_connection();

        return $tsd;
    }

    public function md_tool_func($model, $active, $str_id) {
        $done = false;

        switch ($active) {
            case 'true':
            case 'false':
                $done = $model->model_tool_set_status(prefix . "modules", $active, " ID IN(" . trim($str_id) . ")");
                break;

            case 'delete':
                $arr = explode(',', $str_id);
                if (!empty($arr)) {
                    foreach ($arr as $id) {
                        $img = $this->mda_art_get_pic($id);
                        if (!empty($img))
                            unlink(PATH_BASE . 'upload/images/moduless/' . $img);
                    }
                }
                $done = $model->model_tool_delete(prefix . "modules", " ID IN(" . trim($str_id) . ")");
                $done = $model->model_tool_delete(prefix . "modules_lang", " modulesID IN(" . trim($str_id) . ")");

                break;
        }

        return $done;
    }

    public function md_modules_update_Status_Module($id, $status) {
        $tb_modules = '';

        $tb_modules = prefix . "modules_lang";

        $sql = "UPDATE  `$tb_modules`  SET  `Status` = '$status'  WHERE ModulesID in ($id)";

        $row = parent::mysql_update_insert($sql);

        return $row;
    }

    public function md_modules_delete_Module($id) {
        $tb_modules = '';
        $tb_modules_lang = '';
        $rs = false;
        $rsl = false;

        $tb_modules = prefix . "modules";
        $tb_modules_lang = prefix . "modules_lang";
        $tb_modules_position = prefix . "modules_position";
        
        $sql = "DELETE FROM `$tb_modules` WHERE `ID` in ($id)";
        $rs = parent::mysql_delete($sql);

        if ($rs) {
            $sql = "DELETE FROM `$tb_modules_lang`  WHERE  `ModulesID` in ($id) ";
            $rsl = parent::mysql_delete($sql);

            $sql_del = "DELETE FROM `$tb_modules_position`  WHERE  `ModuleID` in ($id) ";
            $rsl = parent::mysql_delete($sql_del);
        }
        parent::mysql_close_connection();
        return $rsl;
    }

    public function md_modules_get_list_lang($id = 0) {
        $strSql = "";
        $tb_language = '';

        $tb_language = prefix . "language";

        if ($id != 0) {
            //dung khi get dung lang theo id 
        } else {
            $strSql = "SELECT 
                            lag.ID,
                            lag.`Lang`, 
                            lag.`DirName`
                        FROM                                                                     
                             " . $tb_language . " AS lag
                        WHERE
                            lag.Status = 'true' ";
        }

        $row = parent::mysql_fetch_all_rows($strSql);
        parent::mysql_close_connection();
        return $row;
    }

    public function md_modules_add_module($module_name, $app, $status) {
        $id_module = $this->md_modules_get_max_id_module();

        $id_new = $id_module[0]['max(ID)'] + 1;

        if ($module_name != '' and $app != '' and $status != '') {
            $sql = "INSERT INTO `" . prefix . "modules` (`ID`,`Name`, `AppID`) 
                VALUES ($id_new,'" . $module_name . "','" . $app . "',)";

            $rsmod = $this->mysql_update_insert($sql);
            $idModule = mysql_insert_id();
        } else {
            $rsmod = false;
        }
        if ($rsmod) {
            $rs = $idModule;
        } else {
            $rs = false;
        }

        return $rs;
    }

    public function md_modules_add_module_detail($data) {

        $moduleid = $data['id_mod_new'];
        $module_title = $data['ModuleTitle'];
        $list_lang = $data['LangID'];
        $notes = $data['Notes'];
      
        $status = $data['Status'];


        $sql = '';
        $sql = "INSERT INTO `" . prefix . "modules_lang` (`ModulesID`,`LangID`, `ModuleTitle`,  `Status`, `Notes`) 
                VALUES ('" . $moduleid . "','" . $list_lang . "', '" . $module_title . "','" . $status . "','" . $notes . "')";

        $rsmol = $this->mysql_update_insert($sql);

        //neu add o ban lang khong thanh cong thi se delete data vua add vao o ban module
        if ($rsmol == false) {
            $this->md_modules_delete_Module($idModule);
        }

        return $rsmol;
    }

    public function md_modules_get_max_id_module() {
        $strSql = "";
        $tb_module = '';
        $tb_module_lang = '';

        $tb_module = prefix . "modules";
        $tb_module_lang = prefix . "modules_lang";

        $strSql = "SELECT 
                        max(ID)                        
                    FROM                                                                     
                    " . $tb_module;

        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

    public function md_modules_modules_get_list_module_by_appid($id = 0) {
        $tb_module = prefix . "modules";
        $tb_module_lang = prefix . "modules_lang";
        $tb_app = prefix . 'categories';
        $strSql = "SELECT 
                        mou.ID,
                        mou.`Name`, 
                        mol.`Status`, 
                        mou.AppID, 
                        mol.ModuleTitle,                       
                        app.App,                        
                        mol.StrConfig, 
                        mol.Notes
                    FROM                                                                     
                       " . $tb_module . " AS mou                                                    
                        INNER JOIN " . $tb_module_lang . " AS mol 
                            ON mou.ID = mol.ModulesID and 
                            mol.LangID = " . LANG . " and 
                            mou.AppID in( " . $id . " )"
                . " INNER JOIN " . $tb_app . " AS app ON mou.AppID = app.ID";

        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

    public function md_modules_get_module_lang($id = 0, $idlang = 0) {
        $strSql = "";
        $tb_module = prefix . "modules";
        $tb_module_lang = prefix . "modules_lang";

        $strSql = "SELECT 
                        mol.*
                    FROM                                                                     
                       " . $tb_module_lang . " AS mol 
                    WHERE mol.ModulesID = $id  and mol.LangID = $idlang";

        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

    public function md_modules_update_Module($data, $id) {
        $tb_modules = '';
        $tb_modules_lang = '';
        $rsApl = '';
        $rsApp = '';
        if (isset($data) && !empty($data)) {
            $tb_modules = prefix . "modules";
            $tb_modules_lang = prefix . "modules_lang";
            $Name = $data['Name'];
            $ModuleTitle = $data['ModuleTitle'];
            $Status = $data['Status'];
            
            $Notes = $data['Notes'];
            $LangID = $data['LangID'];
            $sql = "UPDATE `$tb_modules` 
                    SET 
                        `Name`='$Name'
                    WHERE `ID`in ('$id')";

            $rsApp = parent::mysql_update_insert($sql);
            if ($rsApp) {
                $sql = '';
                $sql = "UPDATE `$tb_modules_lang` 
                        SET 
                            `ModuleTitle`='$ModuleTitle',
                            
                            `Notes`='$Notes' , 
                            `Status`='$Status ' 
                        WHERE 
                            `ModulesID`in ('$id')
                            and `LangID`in ('$LangID')";
                $rsApl = parent::mysql_update_insert($sql);
            }
        }
        return $rsApl;
    }

    public function md_modules_get_list_module($id = 0, $idlang = 0) {
        $strSql = "";
        $tb_module = '';
        $tb_module_lang = '';

        $tb_lang = prefix . 'language';
        $tb_module = prefix . "modules";
        $tb_module_lang = prefix . "modules_lang";
        if ($id != 0 && $idlang == 0) {
            $strSql = "SELECT 
                        mou.ID,
                        mou.`Name`, 
                        mol.`Status`, 
                        mou.AppID, 
                        mol.ModuleTitle,                     
                        mol.StrConfig, 
                        mol.Notes
                    FROM                                                                     
                       " . $tb_module . " AS mou                                                    
                        INNER JOIN " . $tb_module_lang . " AS mol ON mou.ID = mol.ModulesID and mol.LangID = " . LANG . " and mou.ID in( " . $id . " )"
                    . " INNER JOIN " . $tb_lang . " AS lag ON lag.ID = mol.LangID";
        }
        if ($id == 0 && $idlang == 0) {//truong hop id va idlang deu = 0
            $strSql = "SELECT 
                            mou.ID,
                            mou.`Name`, 
                            mol.`Status`, 
                            mol.ModuleTitle, 
                           
                            mol.StrConfig, 
                            mol.Notes
                        FROM                                                                     
                        " . $tb_module . " AS mou                                                    
                        INNER JOIN " . $tb_module_lang . " AS mol ON mou.ID = mol.ModulesID and mol.LangID =  " . LANG;
        }
        if ($id == 0 && $idlang != 0) {
            $strSql = "SELECT 
                            mou.ID,
                            mou.`Name`, 
                            mol.`Status`, 
                            mol.ModuleTitle, 
                           
                            mol.StrConfig, 
                            mol.Notes
                        FROM                                                                     
                        " . $tb_module . " AS mou                                                    
                        INNER JOIN " . $tb_module_lang . " AS mol ON mou.ID = mol.ModulesID and mol.LangID = " . LANG
                    . " INNER JOIN " . $tb_lang . " AS lag ON lag.ID = mol.LangID and lag.ID in (" . $idlang . " )";
        }
        if ($id != 0 && $idlang != 0) {
            $strSql = "SELECT 
                            mou.ID,
                            mou.`Name`, 
                            mol.`Status`, 
                            mol.ModuleTitle, 
                          
                            mol.StrConfig, 
                            mol.Notes
                        FROM                                                                     
                        " . $tb_module . " AS mou                                                    
                        INNER JOIN " . $tb_module_lang . " AS mol ON mou.ID = mol.ModulesID and mou.ID in( " . $id . " )"
                    . " INNER JOIN " . $tb_lang . " AS lag ON lag.ID = mol.LangID and lag.ID in (" . $idlang . " )";
        }

        $row = parent::mysql_fetch_all_rows($strSql);
        return $row;
    }

}